package com.huanshi.module.ums.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanshi.common.CommonResult;
import com.huanshi.entity.UmsAdmin;
import com.huanshi.module.ums.param.LoginParam;
import com.huanshi.module.ums.param.PasswordParam;
import com.huanshi.service.UmsAdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 幻世
 * @since 2023-09-13
 */
@RestController
@RequestMapping("api/ums/admin")
public class UmsAdminUserController {

    @Resource
    private UmsAdminService adminService;

    // 新增或者更新
    @PostMapping
    public CommonResult save(@RequestBody UmsAdmin user) {

        return CommonResult.success(adminService.saveOrUpdate(user));
    }

    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable Integer id) {
        return CommonResult.success(adminService.removeById(id));
    }

    @PostMapping("/del/batch")
    public CommonResult deleteBatch(@RequestBody List<Integer> ids) {
        return CommonResult.success(adminService.removeByIds(ids));
    }

    @GetMapping
    public CommonResult findAll() {
        return CommonResult.success(adminService.list());
    }

    @GetMapping("/{id}")
    public CommonResult findOne(@PathVariable Integer id) {
        return CommonResult.success(adminService.getById(id));
    }

    @GetMapping("/info")
    public CommonResult userInfo(@RequestParam("token") String token) {
        return CommonResult.success(adminService.getUserInfo(token));
    }

    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParam loginParam){
        return adminService.login(loginParam);
    }

    @PostMapping("/password")
    public CommonResult updatePassword(@RequestBody PasswordParam passwordParam){
        return adminService.updatePassword(passwordParam);
    }
    @GetMapping("/list")
    public CommonResult findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
//        QueryWrapper<UmsAdmin> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
        return CommonResult.success(adminService.page(new Page<>(pageNum, pageSize)));
    }
}

